<?php
//商品类别管理
namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Requests;

class MinfoController extends CommonController
{
    //商品信息管理浏览
    public function index()
    {
		$s = \DB::table("m_search")->get();
		$db = \DB::table("m_info");
		//判断并封装搜索条件
		$params = array();
		if(!empty($_GET['name'])){
			$db->where("name","like","%{$_GET['name']}%");
			$params['name'] = $_GET['name'];  //维持搜索条件
		}
		$m = $db->orderBy('gid', 'asc')->paginate(7);
		$g = \DB::table("m_genre")->get();
		$p = \DB::table("m_pag")->get();
		return view("admin.minfo.index",['list'=>$s,'list1'=>$m,'list2'=>$g,'list3'=>$p,'params'=>$params]);
    }
	//商品信息管理添加操作界面
    public function create()
    {
	   $f = \DB::table("m_forum")->get();
	   $g = \DB::table("m_genre")->get();
	   $p = \DB::table("m_pag")->get();
	   return view('admin.minfo.add',['forum'=>$f,'genre'=>$g,'pag'=>$p]);
    }
	//商品信息管理后台添加
    public function store(Request $request)
    {
		$m = ['fid'=>$request->fid,'gid'=>$request->gid,'name'=>$request->name,'brief'=>$request->brief,'number'=>$request->number,
				'status'=>$request->status,'content'=>$request->content];
		$mid = \DB::table("m_info")->insertGetId($m);
		$s['mid'] = $mid;
		$s = $request->except(['_token','name','brief','number','status','activity','content']);
		
		\DB::table("m_search")->insert($s);
		//判断
		if($mid>0){
			$info = "商品信息添加成功！";
		}else{
			$info = "商品信息添加失败！";
		}
        return redirect('admin/minfo')->with("err",$info);
    }
    public function show($id)
    {
		//响应结果
		$g = \DB::table("m_genre")->where('fid',$id)->get();
		foreach($g as $k => $v){
			$b[$k] = $v->id.'=>'.$v->name;
		}
		echo json_encode($b);
    }
	public function find()
	{
		//响应结果
		$id = $_GET['gid'];
		$s = \DB::table("m_pag")->where('gid',$id)->where('field','!=','price')->get();
		foreach($s as $k => $v){
			$b[$k] = " <div class='form-group'>
                      <label for='inputEmail3' class='col-sm-2 control-label'>".$v->name."</label>
                      <div class='col-sm-4'>
                        <input type='text' class='form-control' name='".$v->field."' placeholder='".$v->name."'>
                      </div>
                    </div>";			
		}
		echo json_encode($b);
	}
    public function edit($id)
    {
		$f = \DB::table("m_forum")->get();
		$g = \DB::table("m_genre")->get();
		$p = \DB::table("m_pag")->get();
		$m = \DB::table("m_info")->where('mid',$id)->first();
		$s = \DB::table("m_search")->where('mid',$id)->first();
		return view('admin.minfo.edit',['forum'=>$f,'genre'=>$g,'minfo'=>$m,'search'=>$s,'pag'=>$p]);
    }
    public function update(Request $request, $id)
    {
		$data = $request->all();
		$s = $request->except(['_token','_method','name','brief','number','status','activity','content']);
		$m = ['fid'=>$request->fid,'gid'=>$request->gid,'name'=>$request->name,'brief'=>$request->brief,
				'number'=>$request->number,'status'=>$request->status,'content'=>$request->content];
		//商品不能由其他状态改为新款发布 视为下架
		if($m['status'] == "wait"){
			$m['status'] = "off";
		}
		\DB::table('m_search')->where('mid',$id)->update($s);
		\DB::table('m_info')->where('mid',$id)->update($m);
		$info = "商品信息修改成功！";
		return redirect('admin/minfo')->with("err",$info);
    }
	public function destroy($id)
    {
		$data = \DB::table("m_info")->where('mid',$id)->first();
		//判断商品信息下是否没有下属商品
		if($data->status == "off"){
			\DB::table('m_genre')->where('id', $id)->delete();
			$info = "商品信息删除成功！";
		}else{
            $info = "商品没有下架，无法删除！";
        }
        return redirect('admin/minfo')->with("err",$info);
    }
}
